<?php
/**
 * html_helper.class.php
 *
 * HTML tags renderer
 *
 * @version:	0.1
 * @package:	core
 * @author:		alex_ez
 * @created:	 2010
 *
 * @copyright:	(c) 2008 flamebox.ru. All rights reserved.
 */

load_helper( 'url' );

// basic ~~~

function html_escape( $s )
{
	return htmlentities( $s, ENT_QUOTES, 'UTF-8' );
}

function html_attributes( $atts )
{
	$atts = (array)($atts);
	$out = '';
	
	foreach( $atts as $a => $v )
	if( $a != 'text' && $a != 'html' && !is_null( $v ) )
		$out .= sprintf( '%s="%s" ', $a, html_escape( $v ) );
	
	return $out;
}

function html_open_element( $el, $atts = array( ) )
{
	$el = (string)($el);
	
	$out = $el.' ';
	$out .= html_attributes( $atts );
	
	return '<'.$out.'>';
}

function html_close_element( $el )
{
	$el = (string)($el);
	return "</$el>";
}

function html_element( $el, $atts = array( ) )
{
	$out = html_open_element( $el, $atts );
	$out .= isset( $atts['text'] ) ? html_escape( $atts['text'] ) : '';
	$out .= isset( $atts['html'] ) ? $atts['html'] : '';
	$out .= html_close_element( $el );
	return $out;
}

function html_empty_element( $el, $atts = array( ) )
{
	$el = (string)($el);
	$atts = html_attributes( $atts );
	return sprintf( "<%s />", trim( $el.' '.$atts ) );
}

// ~~~ basic




function html_heading( $data = '', $h = 1, $is_text = false )
{
	$h = min(max($h,1),6);
	return html_element( 'h'.$h, array( $is_text ? 'text' : 'html' => $data ) );
}

function html_br( $class = '', $atts = array( ) )
{
	$atts = (array)$atts;
	if( !empty( $class ) )
		$atts['class'] = is_array( $class ) ? join( ' ', $class ) : (string)($class);
	return html_empty_element( 'br', $atts );
}

function html_img( $atts = array( ) )
{
	if( !is_array( $atts ) )
	{
		$atts = (string)($atts);
		if( empty( $atts ) ) return;
		
		$atts = array( 'src' => $atts, 'alt' => $atts );
	}
	
	if( !isset( $atts['src'] ) )
		$atts['src'] = $src;
	if( !isset( $atts['alt'] ) )
		$atts['alt'] = '';
	
	$atts['src'] = ( strpos($atts['src'], '://') === false )
		? site_url( $atts['src'] ) : $atts['src'] ;
	
	return html_empty_element( 'img', $atts );
}

function html_a( $href, $text = '', $class = '', $title = '', $atts = array( ) )
{
	$args = func_get_args( );
	$args_index = array( 'href', 'text', 'class', 'title' );
	
	$atts = end( $args );
	if( is_array( $atts ) ) $args[key($args)] = null; else $atts = array( );
	
	foreach( $args_index as $k => $v ) if( !isset( $args[$k] ) || is_array( $args[$k] ) ) $args[$k] = null;
	$args = array_combine( $args_index, $args );
	
	foreach( $args as $k => $v )
		$atts[$k] = !empty( $v ) ? $v : ( isset( $atts[$k] ) ? $atts[$k] : null );
	
	if( isset( $atts['class'] ) && !empty( $atts['class'] ) )
		$atts['class'] = is_array( $class ) ? join( ' ', $class ) : (string)($class);
	
	if( isset( $atts['href'] ) )
		$atts['href'] = ( strpos($atts['href'], '://') === false ) ? site_url( $atts['href'] ) : $atts['href'] ;
	
	return html_element( 'a', $atts );
}

function html_doctype( $type = 'html5' )
{
	$doctypes = array( );
	$doctypes['xhtml11'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
	$doctypes['xhtml1-strict'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
	$doctypes['xhtml1-trans'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
	$doctypes['xhtml1-frame'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
	$doctypes['html4-strict'] = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
	$doctypes['html4-trans'] = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
	$doctypes['html4-frame'] = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
	$doctypes['html5'] = '<!DOCTYPE html>';
	
	if( !isset( $doctypes[$type] ) )
		$type = 'html5';
	
	return $doctypes[$type];
}


function html_cycle( $cycle, $name = null )
{
	$name = !is_null( $name ) ? $name : md5( $cycle );
	$items = explode( ',', $cycle );
	
	static $cycles;
	if( !isset( $cycles[$name] ) ) $cycles[$name] = 0;
	
	$current = $cycles[$name]%count($items);
	$r = $items[$current];
	
	$cycles[$name]++;
	return $r;
}

